package com.dustedpixels.common;

/**
 * Binary utilities.
 */
public final class Binary {
  /**
   * Returns the least significant byte of given word.
   * 
   * @param word The word
   * @return The byte
   */
  public static byte lo(short word) {
    return (byte) word;
  }

  /**
   * Returns the most significant byte of given word.
   * 
   * @param word The word
   * @return The byte
   */
  public static byte hi(short word) {
    return (byte) (word >>> 8);
  }

  /**
   * Returns a word composed from high and low bytes.
   * 
   * @param hi The hi byte
   * @param lo The lo byte
   * @return The word
   */
  public static short word(byte hi, byte lo) {
    return (short) ((hi << 8) | (lo & 0xFF));
  }

  public static byte sl(byte b) {
    return (byte) (b << 1);
  }

  public static byte sr(byte b) {
    return (byte) ((b & 0xFF) >>> 1);
  }

  public static byte rl(byte b) {
    return (byte) ((b << 1) | (b & 0xFF >>> 7));
  }

  public static byte rr(byte b) {
    return (byte) (((b & 0xFF) >>> 1) | (b << 7));
  }
}
